using System.Threading.Channels;
using mapshiftApplication;
using mapshiftApplication.Config;
using mapshiftApplication.Helpers;
using mapshiftApplication.Hubs;
using mapshiftApplication.Repositories;
using mapshiftApplication.Services;
using Microsoft.AspNetCore.SignalR;
using Oracle.ManagedDataAccess.Client;
using Quartz;
using Quartz.Impl;
using Quartz.Simpl;
using IOracleConnectionFactory = mapshiftApplication.IOracleConnectionFactory;

var builder = WebApplication.CreateBuilder(args);

var environment = builder.Environment;

if (environment.IsDevelopment())
{
    Console.WriteLine("��������");
}
else if (environment.IsProduction())
{
    Console.WriteLine("��������");
}


builder.WebHost.UseUrls("http://0.0.0.0:8080");



// ���ӷ���
builder.Services.AddControllers();
// ע��SignalR�ķ���
builder.Services.AddSignalR();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// �� ע�� Quartz���ٷ���ʽ��
builder.Services.AddQuartz(q =>
{
    q.UseJobFactory<MicrosoftDependencyInjectionJobFactory>();
});

// �� �� Quartz ������������������/ֹͣ
builder.Services.AddQuartzHostedService(opt => { opt.WaitForJobsToComplete = true; });


builder.Services.AddSingleton<TaskService>();
builder.Services.AddSingleton(Channel.CreateUnbounded<ushort>());
builder.Services.AddScoped<DatFileUpdaterService>(); 
builder.Services.AddScoped<StdfAnalysisService>(); 
builder.Services.AddScoped<ReportService>(); 
builder.Services.AddScoped<MapShiftService>(); 
builder.Services.AddScoped<ScreenService>(); 
builder.Services.AddScoped<CheckMapService>(); 
builder.Services.AddScoped<ITsmcConfigRepository, TsmcConfigRepository>();
builder.Services.AddScoped<TsmcConfigService>();
builder.Services.AddHttpClient(nameof(StdfAnalysisService)); 
builder.Services.AddInfrastructure(builder.Configuration);


// CORS ���ã���ȫ�棩
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowFrontend",
        policy => policy
            .WithOrigins("http://10.20.5.21:8081", "http://localhost:8848","http://127.0.0.1:5501","http://10.20.5.21:8082", "http://10.20.5.21:8083") // �滻Ϊ���ǰ��ʵ�ʵ�ַ
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials()); // ���迪��
});

// ���ݿ�����
// builder.Services.AddScoped<OracleConnection>(sp =>
// {
//     var configuration = sp.GetRequiredService<IConfiguration>();
//     string connectionString = configuration.GetConnectionString("DefaultConnection")!;
//     return new OracleConnection(connectionString);
// });
builder.Services.AddSingleton<IOracleConnectionFactory, OracleConnectionFactory>();


builder.Services.Configure<ExternalToolOptions>(builder.Configuration.GetSection("ExternalTools"));

var app = builder.Build();

// �м������
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseRouting();

app.UseCors("AllowFrontend"); // ȷ��˳����ȷ
app.UseAuthorization();
app.MapControllers();
app.MapHub<ProgressHub>("/progressHub");
app.UseStaticFiles();

//  ������ֱ�Ӵ��� channel ʵ�������κ����װ��
var _channel = app.Services.GetRequiredService<Channel<ushort>>();

// ������̨�����ȡ channel �����͸�ǰ��
var hubContext = app.Services.GetRequiredService<IHubContext<ProgressHub>>();

_ = Task.Run(async () =>
{
    LogHelper.Info("��̨��������������");

    try
    {
        await foreach (var progress in _channel.Reader.ReadAllAsync()) // ʹ����ͬ�� _channel ʵ��
        {
            await hubContext.Clients.All.SendAsync("ReceiveProgress", progress);
        }
    }
    catch (Exception ex)
    {
        LogHelper.Error($"[���������쳣]��{ex.Message}");
    }
});
app.Run();